<template>
  <view v-if="showLoginModal" class="login-modal">
    <view class="content">
      <view class="title">授权登录周期订</view>
      <button type="primary" open-type="getUserInfo" @click="getLogin">登录</button>
    </view>
  </view>
</template>

<script>
import {  
  mapState, mapMutations
} from 'vuex' 
export default {
  computed: {
    ...mapState(['showLoginModal']),
  },
  methods: {
    ...mapMutations(['login', 'hideLogin']),
    async getLogin() {
      const _this = this
      uni.login({
        provider: 'weixin',
        success: function(loginRes) {
          // 获取用户信息
          uni.getUserInfo({
            provider: 'weixin',
            success(infoRes) {
              console.log('用户openId: ', infoRes)
              const userInfo = infoRes.userInfo
              userInfo.openId = 'eqdasdasda'
              userInfo.mobile = '18888888888'
              _this.$api.json('userInfo').then(result => {
                if(result.status === 1){
                  _this.login(userInfo)
                  _this.hideLogin()
                } else {
                  _this.$api.msg(result.msg)
                }
              })
            },
            complete() {
              _this.hideLogin()
            }
          })
        }
      })
    },
  }
}
</script>

<style>

</style>
